﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：功能模块管理页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;
using System.Data.SqlClient;

public partial class Whir_System_Module_Contents_ModelManage : System.Web.UI.Page
{
    protected string action;
    protected int modelID;
    protected string url = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        action = NetRequest.GetQueryString("Action");
        modelID = NetRequest.GetQueryInt("ModelID", 0);
        url = Server.UrlEncode(NetRequest.GetUrl());

        if (action == "Delete")
        {
            DeleteModel(modelID);
        }

        if (!IsPostBack)
        {
            ddlSelect.DataSource = new Whir.BLL.Whir_Module().GetList();
            ddlSelect.DataTextField = "moduleName";
            ddlSelect.DataValueField = "moduleMark";
            ddlSelect.DataBind();
            BindData();
        }
    }

    /// <summary>
    /// 删除功能模块
    /// </summary>
    private void DeleteModel(int modelID)
    {
        SqlParameter[] parameters = { new SqlParameter("@modeID", SqlDbType.Int) };
        parameters[0].Value = modelID;

        try
        {
            Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
            Whir.Model.Whir_Model model = bll.GetModel(modelID);
            string tableName = model.tableName;

            Whir.SQLServerDAL.DbHelperSQL.ExecuteSql("DROP TABLE " + tableName);
            Whir.SQLServerDAL.DbHelperSQL.RunProcedure("Proc_DeleteModel", parameters);
            JsHelper.redirect(Page, "功能模块删除成功！", "ModelManage.aspx");
        }
        catch (Exception ex)
        {
            JsHelper.redirect(Page, "功能模块删除失败！", "ModelManage.aspx");
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        string where = "";
        if (this.ddlSelect.SelectedItem.Value != "all")
        {
            where = "moduleMark='" + this.ddlSelect.SelectedItem.Value + "'";
        }
        DataSet ds = Whir.SQLServerDAL.DbHelperSQL.GetList("Whir_Model", "modelID", "*", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex - 1, where, "", "CreateDate DESC");
        int rowCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
        AspNetPager1.RecordCount = rowCount;

        if (rowCount > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
            divMessage.Visible = false;
        }
        else
        {
            GridView1.Controls.Clear();
            divMessage.Visible = true;
        }
    }

    //根据模块查看功能模块
    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }

    //功能模块添加
    protected void btnAddModel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Model.aspx?Action=Add&url=" + Server.UrlEncode(NetRequest.GetUrl()));
    }

    //功能模块导出
    protected void btnExport_Click(object sender, EventArgs e)
    {
        int[] itemIds = this.GridView1.CheckedIndexs;
        if (itemIds.Length > 0)
        {
            string msg = "",fileurl;
            Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
            Whir.Model.Whir_Model model = new Whir.Model.Whir_Model();

            foreach (int itemId in itemIds)
            {
                model = bll.GetModel(itemId);
                msg += model.modelName + ",";
                ModelTemplateHelper.Export(itemId, model.modelName + "." + model.createDate.ToString("yyyy-MM-dd-HH-mm-ss"));//DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")

                fileurl = Whir.Common.Settings.GetSystemPath() + "App_Code/Model/";
                ModelTemplateHelper.CreateModelEntity(itemId, Page.MapPath(fileurl));
            }

            msg = msg.TrimEnd(',');
            
            JsHelper.Alert(Page, msg + "导出成功！");
        }
        else
            JsHelper.Alert(Page, "请选择要导出的功能模块！");
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
}